package wang.aweb.childcar.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wang.aweb.childcar.exception.CarException;
import wang.aweb.childcar.exception.CarExceptionEnum;
import wang.aweb.childcar.model.dao.FenceMapper;
import wang.aweb.childcar.model.pojo.Fence;
import wang.aweb.childcar.model.pojo.Zuobiao;
import wang.aweb.childcar.model.req.FenceReq;
import wang.aweb.childcar.model.vo.FenceVO;
import wang.aweb.childcar.service.FenceService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FenceServiceImpl implements FenceService {
    @Autowired
    private FenceMapper fenceMapper;
    @Override
    public PageInfo selectList(Integer page, Integer limit){
        PageHelper.startPage(page,limit);
        List<Fence> fenceList=fenceMapper.selectList();
        List<FenceVO> fenceVOList=fenceListToFenceVOList(fenceList);
        PageInfo fencePageInfo=new PageInfo<>(fenceList);

        fencePageInfo.setList(fenceVOList);
        return fencePageInfo;
    }
    @Override
    public void add(FenceReq fenceReq) {
        Fence fence=new Fence();
        Zuobiao[] fenceArray=fenceReq.getFence();
        String fenceStr="[";
        for(Zuobiao zuobiao:fenceArray){
            fenceStr=fenceStr+zuobiao.toString()+",";
        }
        fenceStr=fenceStr.substring(0,fenceStr.length()-1);
        fenceStr=fenceStr+"]";
        fence.setFence(fenceStr);
        fence.setAddress(fenceReq.getAddress());
        fence.setCreateTime(new Date());
        int count=fenceMapper.insert(fence);
        if(count==0){
            throw new CarException(CarExceptionEnum.INSERT_FAILED);
        }

    }
    public void update(FenceReq fenceReq){
        Fence fence=new Fence();
        Zuobiao[] fenceArray=fenceReq.getFence();
        String fenceStr="[";
        for(Zuobiao zuobiao:fenceArray){
            fenceStr=fenceStr+zuobiao.toString()+",";
        }
        fenceStr=fenceStr.substring(0,fenceStr.length()-1);
        fenceStr=fenceStr+"]";
        fence.setFence(fenceStr);
        fence.setAddress(fenceReq.getAddress());
        fence.setId(fenceReq.getId());
        int count=fenceMapper.updateByPrimaryKeySelective(fence);
        if(count==0){
            throw new CarException(CarExceptionEnum.UPDATE_FAILED);
        }

    }
    @Override
    public List<Fence> getAll(){
        List<Fence> fenceList=fenceMapper.selectList();
        return fenceList;
    }
    @Override
    public void delete(Integer id){

        int count=fenceMapper.deleteByPrimaryKey(id);
        if(count==0){
            throw new CarException(CarExceptionEnum.DELETE_FAILED);
        }
    }
    @Override
    public Fence getInfo(int id) {
        Fence fence=fenceMapper.selectByPrimaryKey(id);
        return fence;
    }
    public List<FenceVO> fenceListToFenceVOList(List<Fence> fenceList){
        List<FenceVO> fenceVOList=new ArrayList<>();
        for (int i = 0; i < fenceList.size(); i++) {
            Fence fence =  fenceList.get(i);
            fenceVOList.add(getFenceVO(fence));
        }
        return fenceVOList;
    }
    private FenceVO getFenceVO(Fence fence){
        if(fence==null){
            return null;
        }else{
            FenceVO fenceVO=new FenceVO();
            BeanUtils.copyProperties(fence,fenceVO);
            fenceVO.setCreate_time(fence.getCreateTime());
            return fenceVO;
        }
    }
}
